//
//  JCClubInfoPlanWeekCell.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/22.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCClubInfoPlanWeekCell.h"
#import "JCClubInfoWeekPlanCircleCell.h"
#import "UIView+JCDrawTool.h"

@interface JCClubInfoPlanWeekCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *circleView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIButton *actionButton;
@property (strong, nonatomic) UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UIView *page_01;
@property (weak, nonatomic) IBOutlet UIView *page_02;
@property (weak, nonatomic) IBOutlet UIView *page_03;

@end

@implementation JCClubInfoPlanWeekCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [_circleView layoutIfNeeded];
    if (_collectionView == nil) {
        
        if (IS_IPHONE_5) {
            CGRect frame = _circleView.frame;
            frame.size = CGSizeMake(frame.size.width, frame.size.height + 30);
            _circleView.frame = frame;
        }
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

        CGSize size = _circleView.bounds.size;
        layout.itemSize = size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, size.height) collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerNib:[UINib nibWithNibName:@"JCClubInfoWeekPlanCircleCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"JCClubInfoWeekPlanCircleCell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_circleView addSubview:_collectionView];
        [_collectionView reloadData];
        if (_sportItems.count == 3) {
            [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        }
    }

    _actionButton.layer.cornerRadius = 3;
    _actionButton.layer.masksToBounds = YES;
}

- (void)setWatchType:(WatchClubType)watchType{
    _watchType = watchType;
    if (_watchType == WatchClubTypeApplyForAmission) {
        [_actionButton setTitle:@"APPLY FOR ADMISSION" forState:UIControlStateNormal];
    }else if (_watchType == WatchClubTypeMyMemberGeneral){
        [_actionButton setTitle:@"VIEW OTHER MEMBERS" forState:UIControlStateNormal];
    }else if (_watchType == WatchClubTypeMyMemberGeneral){
        [_actionButton setTitle:@"VIEW OTHER MEMBERS" forState:UIControlStateNormal];
    }else if (_watchType == WatchClubTypeOtherMember){
        [_actionButton setTitle:@"VIEW OTHER MEMBERS" forState:UIControlStateNormal];
    }
}

- (void)setSportItems:(NSArray<JCClubInfoSportItem *> *)sportItems{
    _sportItems = sportItems;
    if (sportItems.count < 3) {
        _page_01.hidden = YES;
        _page_02.hidden = YES;
        _page_03.hidden = YES;
    }else{
        _page_01.hidden = NO;
        _page_02.hidden = NO;
        _page_03.hidden = NO;
    }
    [_collectionView reloadData];
}

- (IBAction)buttonAction:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(buttonActionType:)]) {
        [_delegate buttonActionType:_watchType];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _collectionView) {
        NSInteger page = (scrollView.contentOffset.x + 0.5*kYFScreenWidth) / kYFScreenWidth;
        [UIView animateKeyframesWithDuration:.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
            if (page == 0) {
                _page_01.alpha = 1;
                _page_02.alpha = 0.3;
                _page_03.alpha = 0.3;
            }else if (page == 1){
                _page_01.alpha = 0.3;
                _page_02.alpha = 1;
                _page_03.alpha = 0.3;
            }else if (page == 2){
                _page_01.alpha = 0.3;
                _page_02.alpha = 0.3;
                _page_03.alpha = 1;
            }
            if (_delegate && [_delegate respondsToSelector:@selector(scrollToIndex:)]) {
                [_delegate scrollToIndex:page];
            }
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _sportItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JCClubInfoWeekPlanCircleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JCClubInfoWeekPlanCircleCell" forIndexPath:indexPath];
    cell.watchType = _watchType;
    cell.sportItem = _sportItems[indexPath.row];
    cell.isWatchMember = _isWatchMember;
    cell.indexPath = indexPath;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

@end
